/* 
When we pass objects down the pipe as pure JSON, they don't come out quite
right - they need to be turned into real Objects instead of just dictionaries.
*/

OUTPUT_FORMAT = "text/javascript"

function _PTJS_Decoder(objs) {
  // Objs could be anything - a string, dictionary, array... <sigh>
  var simple = {"string":true,"number":true,"boolean":true,"function":true}
  if(typeof objs in simple) {
    return objs
  }
  // Special case for "null"
  if(typeof objs == "object" && objs == null) {
    return objs
  }
  // A weak way of detecting arrays - maybe find something better?
  if(typeof objs == "object" && objs.length) {
    for(var i=0;i<objs.length;i++) {
      objs[i] = _PTJS_Decoder(objs[i]);
    }
    return objs
  }
  // Now we *really* have an "object" (i.e. dictionary)
  if(typeof objs == "object") {
    // First, let's recursively check all the values, in case *they* are objects
    for(var i in objs) {
      objs[i] = _PTJS_Decoder(objs[i])
    }
    // Now, let's see if this is a PTJS object that needs casting
    if(objs["_PTJS_ObjectName"]) {
      var obj_name = objs["_PTJS_ObjectName"]
      delete(objs["_PTJS_ObjectName"])
      var obj = eval(obj_name);
      var o = new obj(objs);      
      return o;
    }
    else {
      return objs;
    }
  }
  throw Error("Unknown object type:"+objs)
}
